
import pymysql

from erp_fangling_cn import house

# 连接到数据库
conn = pymysql.connect(
    host="43.156.247.50",  # 替换为实际的主机名
    user="root",  # 替换为实际的用户名
    password="520777",  # 替换为实际的密码
    database="house"  # 替换为实际的数据库名
)
cur = conn.cursor()

# 执行查询
cur.execute("select house_id from house.house where create_time is null order by house_id desc;")

# 分批获取数据
batch_size = 100
while True:
    rows = cur.fetchmany(batch_size)
    if not rows:
        break

    # 处理当前批次的数据
    for row in rows:
        house_id = row[0]
        try:
            cursor = conn.cursor()
            
            # 开始查询当前的房产日志信息，并入库
            hou = house.getHouse(house_id)
            createTime = hou['createTimeString']

            # 构建插入数据的 SQL 查询
            sql = "update house.house set create_time = %s where house_id = %s;;"
            values = (
                createTime,
                house_id
            )

            # 执行 SQL 查询
            cursor.execute(sql, values)
            conn.commit()
            cursor.close()
            
            print(f"更新{house_id}成功")
        except Exception as e:
            print(f"未知异常:", str(e))

# 关闭数据库连接
cur.close()
conn.close()
